<template>
  <div class="login-container">
    <div class="login-main">
      <!-- 图片区域 -->
      <div class="login-logo">
        <img src="./../assets/img/logo.png">
      </div>
      <!-- 登录表单区域 -->
      <el-form class="loginForm" ref="loginFormRef" :model="loginForm"
      :rules="loginFormRules" label-position="right" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="buttons" >
          <el-button type="primary" @click="submitLoginForm">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      // 登录表单
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单的验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-5个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-5个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitLoginForm () {
      // 这里是验证表单项是否完整正确填写
      this.$refs.loginFormRef.validate(async valid => {
        if (valid) {
          // 发起请求，使用对象解构的方式把data从结果中解析出来
          const { data: res } = await this.$http.post('login', this.loginForm)
          // console.log(res)
          if (res.meta.status === 200) {
            this.$message.success('登录成功')
            // 将登录的token保存到sessionStorage
            window.sessionStorage.setItem('token', res.data.token)
            // 跳转路由
            this.$router.push('/home')
          } else {
            this.$message.error('登录失败')
          }
        } else {
          return false
        }
      })
    },
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style scoped>
.login-container {
  background: #2b4b6b;
  height: 100%;
}
.login-main {
  background: #ffffff;
  width: 450px;
  height: 300px;
  border-radius: 3px;
  /* 使得居中的方案，这种方法不需要提前知道父容器和子容器的高度和宽度 */
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.login-logo {
  height: 130px;
  width: 130px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #dddddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #ffffff
}
img{
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background: #eeeeee
}
/* 使得按钮居右对齐 */
.buttons {
  display: flex;
  justify-content: flex-end;
}
/* 使得整个表单位于最底部 */
.loginForm {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0px 10px;
  box-sizing: border-box;
}
</style>
